package com.imooc.activity7_workflow;

import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class Part4_Task {

    @Autowired
    private TaskService taskService;

    // 任务查询
    @Test
    public void getTasks(){
        List<Task> list = taskService.createTaskQuery()
                .list();
        list.forEach(task -> {
            System.out.println("Id:" + task.getId());
            System.out.println("Name:" + task.getName());
            System.out.println("Assignee:" + task.getAssignee());
        });
    }


    // 查询我的待办任务
    @Test
    public void getTaskByAssignee(){
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee("wukong")
                .list();
        list.forEach(task -> {
            System.out.println("Id:" + task.getId());
            System.out.println("Name:" + task.getName());
            System.out.println("Assignee:" + task.getAssignee());
        });
    }

    // 执行任务
    @Test
    public void completeTask(){
        taskService.complete("0900ebdd-4d80-11eb-a0bc-c23049e74a6b");
        System.out.println("完成任务");
    }

    // 拾取任务
    @Test
    public void claimTask(){
        Task task = taskService.createTaskQuery()
                .taskId("8c1490ec-4d81-11eb-861c-c23049e74a6b")
                .singleResult();
        taskService.claim("8c1490ec-4d81-11eb-861c-c23049e74a6b", "wukong");
    }

    // 归还与交办任务
    @Test
    public void setTaskAssignee(){
        Task task = taskService.createTaskQuery().taskId("").singleResult();
        taskService.setAssignee("8c1490ec-4d81-11eb-861c-c23049e74a6b", "null"); // 归还候选任务
        taskService.setAssignee("8c1490ec-4d81-11eb-861c-c23049e74a6b", "bajie"); // 交办
    }
}
